﻿<!DOCTYPE html>
<html>
<head>
    <title>OAuth-Server</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <link href="http://g.tbcdn.cn/sj/wqui/1.0.0/css/sui.min.css" rel="stylesheet">
    <script type="text/javascript" src="http://g.tbcdn.cn/sj/lib/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript" src="http://g.tbcdn.cn/sj/wqui/1.0.0/js/sui.min.js"></script>
</head>
<body>
    <div class="sui-container" style="margin-top:10px;margin-left:10px">
        <h2>欢迎使用OAuth-应用注册</h2>
        <div class="sui-msg msg-large msg-block msg-success" style="display:none">
            <div class="msg-con">
                <label></label>
            </div>
            <s class="msg-icon"></s>
        </div>
        <div id="regForm">
            <form class="sui-form form-horizontal" autocomplete="off">
                <div class="control-group">
                    <label for="inputEmail" class="control-label">应用名称：</label>
                    <div class="controls">
                        <input type="text" id="inputApp" placeholder="应用名称" class="input-block-level">
                    </div>
                </div>
                <div class="control-group">
                    <label for="inputPassword" class="control-label">应用回调地址：</label>
                    <div class="controls"><input type="text" id="inputUri" placeholder="应用回调地址"></div>
                </div>
                <div class="control-group">
                    <label class="control-label"></label>
                    <div class="controls">
                        <button type="button" class="sui-btn btn-primary">注册</button>
                    </div>
                </div>
            </form>
        </div>
        
    </div>
    <script>
        $(function () {
            $('.sui-btn').on('click', function () {
                var app = $.trim($('#inputApp').val());
                var url = $.trim($('#inputUri').val());
                if (app.length == 0 || url.length == 0) {
                    alert("应用名称或者应用回调地址没有填写");
                    return;
                }
                if (!IsURL(url)) {
                    alert("回调地址不正确");
                    return;
                }
                $(this).off('click');
                $.post('/do', { action: 'client_register', name: app, url: url }, function (res) {
                    if (res.error == 0) {
                        $('.msg-con label').html("client_id:" + res.data.client_id + "，client_secret:" + res.data.client_secret + "，妥善保存");
                        $('.sui-msg').show();
                        $('#regForm').hide();
                    } else {
                        alert("出错了，稍后再试");
                    }
                });
            });

            function IsURL(url){
                var strRegex = "^((https|http)://)[a-z0-9A-Z]{3}\.[a-z0-9A-Z][a-z0-9A-Z]{0,61}?[a-z0-9A-Z]\.com|net|cn|cc (:s[0-9]{1-4})?/$";
                var re = new RegExp(strRegex);
                if (re.test(url)) {
                    return true;
                } else {
                    return false;
                }
            }
        });
    </script>
</body>
</html>